package com.orangelife.mobile.parkingcard.adapter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.curry.android.adapter.FatherAdapter;
import com.orangelife.mobile.parkingcard.activity.ApplyForParkingCardBeginActivity;

public class CommunityAdapter<T> extends FatherAdapter<T> {

	private Context context;
	private List<Map<String, Object>> list;
	/**
	 * 从哪个地方过来，1是小区，2是楼栋，3是停车场类型,4是房号
	 */
	private int active;
	
	public CommunityAdapter(Context context,
			List<Map<String, Object>> list,int active) {

		this.context = context;
		this.list = list;
		this.active = active;
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public BaseAdapter setList(List<T> list) {
		this.list = (List<Map<String, Object>>) list;
		return CommunityAdapter.this;
	}
	
	@SuppressWarnings("unchecked")
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_parking_card_select_community, null);
			holder.tvText = (TextView) convertView
					.findViewById(R.id.tvText);
			holder.llClick = (LinearLayout) convertView
					.findViewById(R.id.llClick);
			holder.view = convertView.findViewById(R.id.view);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		Map<String,Object> map = list.get(position);
		
		switch (active) {
		case 1:
			holder.tvText.setText(map.get("comm").toString());
			break;
		case 2:
			holder.tvText.setText(map.get("buildingName").toString());
			break;
		case 3:
			holder.tvText.setText(map.get("parkingName").toString());
			break;
		case 4:
			holder.tvText.setText(map.get("apartmentName").toString());
			break;

		default:
			break;
		}
		if(position == list.size()-1){
			holder.view.setVisibility(View.GONE);
		}else{
			holder.view.setVisibility(View.VISIBLE);
		}
		holder.llClick.setOnClickListener(new Listener(map));

		return convertView;
	}
	
	public class Listener implements OnClickListener{
		
		Map<String,Object> maps = new HashMap<String, Object>();
		
		public Listener(Map<String,Object> maps){
			this.maps = maps;
		}

		@Override
		public void onClick(View view) {
			Intent intent = null;
			Bundle bundle = new Bundle();
			bundle.putSerializable("map", (Serializable) maps);
			
			switch (view.getId()) {
			case R.id.llClick:
				switch (active) {
				case 1:
				 intent = new Intent(context, ApplyForParkingCardBeginActivity.class);
				 intent.putExtras(bundle);
				 ((Activity) context).setResult(1000, intent);
					break;
				case 2:
					intent = new Intent(context, ApplyForParkingCardBeginActivity.class);
					intent.putExtras(bundle);
					 ((Activity) context).setResult(2000, intent);
					break;
				case 3:
					intent = new Intent(context, ApplyForParkingCardBeginActivity.class);
					intent.putExtras(bundle);
					 ((Activity) context).setResult(3000, intent);
					break;
				case 4:
					intent = new Intent(context, ApplyForParkingCardBeginActivity.class);
					intent.putExtras(bundle);
					 ((Activity) context).setResult(4000, intent);
					 break;

				default:
					break;
				}
				
				((Activity) context).finish();
				  
				break;

			default:
				break;
			}
		}
		
	}

	class ViewHolder {
		/** 列表内容 */
		private TextView tvText;
		/**列表点击*/
		private LinearLayout llClick;
		/**item底下的view*/
		private View view;
	}

}
